package stark.app.function.download;

import android.net.Uri;
import android.util.Log;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.stark.downloader.Downloader;
import com.stark.downloader.FailCause;

import androidx.annotation.NonNull;
import stark.app.R;
import stark.app.databinding.ActivityDownloadDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;

/**
 * Author：daizhongbin on 2021/4/22
 * Email：1027154338@qq.com
 * Desc:
 */
public class DownloadDemoActivity extends BaseNoModelActivity<ActivityDownloadDemoBinding> {

    private static final String TAG = DownloadDemoActivity.class.getSimpleName();
    private static final String IMG_URL = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2F2c.zol-img.com.cn%2Fproduct%2F124_500x2000%2F748%2FceZOdKgDAFsq2.jpg&refer=http%3A%2F%2F2c.zol-img.com.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1621688493&t=8dfca4e43d72b833006336889d8c3c4a";
    private static final String IMG_URL2 = "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3228549874,2173006364&fm=26&gp=0.jpg";
    private static final String IMG_URL3 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.jj20.com%2Fup%2Fallimg%2F1114%2F0H120154Q6%2F200H1154Q6-6-1200.jpg&refer=http%3A%2F%2Fpic.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1621737673&t=be5cec2726099252eb43cd7d06ae055e";

    @Override
    protected int onCreate() {
        return R.layout.activity_download_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnDownloadPublic.setOnClickListener(v -> {
            download2Public();
        });
        mDataBinding.btnDownloadPrivate.setOnClickListener(v -> {
            download2Private();
        });
        mDataBinding.btnDownloadInterval.setOnClickListener(v -> {
            downloadSetInterval();
        });
    }

    @Override
    protected void initData() {
    }

    private void download2Public() {
        Downloader.newTask(this)
                .url(IMG_URL)
                .saveToPublic(true)
                //Android10及以上，保存到外部公共存储空间需提供文件名称
                .fileName(System.currentTimeMillis() + ".jpg")
                .start(mCallback);
    }

    private void download2Private() {
        Downloader.newTask(this)
                .url(IMG_URL2)
                //不调用或者置为false
                //.saveToPublic(false)
                //保存到应用的私有空间，可以不用提供名称，内部自己会搞定
                //.fileName(System.currentTimeMillis() + ".jpg")
                .start(mCallback);
    }

    private void downloadSetInterval() {
        Downloader.newTask(this)
                .url(IMG_URL3)
                .saveToPublic(true)
                .fileName(System.currentTimeMillis() + ".jpg")
                .minUpdateProgressInterval(100) //进度回调时间为100毫秒
                .start(mCallback);
    }

    private Downloader.ICallback mCallback = new Downloader.ICallback() {
        @Override
        public void onStart(int downloadTaskId) {
        }

        @Override
        public void onProgress(long currentOffset, long totalLength, int percent) {
            mDataBinding.progressBar.setProgress(percent);
        }

        @Override
        public void onComplete(@NonNull Uri uri) {
            mDataBinding.tvUri.setText("保存的文件URI: " + uri.toString());
            Glide.with(mDataBinding.ivImg).load(uri).into(mDataBinding.ivImg);
            mDataBinding.progressBar.setProgress(mDataBinding.progressBar.getMax());
        }

        @Override
        public void onFail(FailCause cause) {
            Log.e(TAG, "cause = " + cause.toString());
            ToastUtils.showShort("下载失败");
        }
    };
}
